﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NullSuicide.Steps
{
    public partial class SuicidePlayers : Step
    {
        Players _players;
        Dictionary<int, Zone> _zones;

        public SuicidePlayers()
        {
            InitializeComponent();
        }

        private void lbItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbItems.SelectedItem != null)
            {
                Item item = (Item)lbItems.SelectedItem;
                HtmlDocument doc = (HtmlDocument)wbItem.Document;

                doc.OpenNew(false);
                doc.Write(item.HTML_BODY);
            }
        }

        private void Addplayers()
        {
            dsPlayers.Clear();
            foreach (Player player in _players.ListOfPlayers)
            {
                if (player.InRaid)
                {
                    dsPlayers.Tables["Players"].Rows.Add(player.ID, player.Name, player.InRaid, player.Position);
                }
            }
        }

        internal override void LoadData(Players players)
        {
            _players = players;
            dsPlayers.Tables["Players"].Clear();

            Addplayers();

            _zones = DataConnector.LoadItems();
            foreach (KeyValuePair<int, Zone> pair in _zones)
            {
                cbZone.Items.Add(pair.Value);
            }
        }

        private void LoadBossCombo(Zone selectedZone)
        {
            cbBoss.Items.Clear();
            foreach (KeyValuePair<int, Boss> pair in selectedZone.Bosses)
            {
                cbBoss.Items.Add(pair.Value);
            }                            
        }

        private void LoadItems(Zone selectedZone, string filter)
        {
            lbItems.Items.Clear();
            AddItems(filter, selectedZone.Items);
        }

        private void LoadItems(Boss selectedBoss, string filter)
        {
            lbItems.Items.Clear();
            AddItems(filter, selectedBoss.Items);
        }

        private void AddItems(string filter, Dictionary<int, Item> items)
        {
            foreach (KeyValuePair<int, Item> pair in items)
            {
                if (filter.Length <= 0 || pair.Value.Name.ToLower().Contains(filter.ToLower()))
                {
                    lbItems.Items.Add(pair.Value);
                }
            }
        }

        private void cbZone_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbBoss.SelectedIndex = -1;
            if (cbZone.SelectedItem != null)
            {
                LoadBossCombo((Zone)cbZone.SelectedItem);
                LoadItems((Zone)cbZone.SelectedItem, "");                
            }
            tbItemFilter.Text = "";
        }

        private void cbBoss_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbBoss.SelectedItem != null)
            {
                LoadItems((Boss)cbBoss.SelectedItem, "");                
            }
            tbItemFilter.Text = "";
        }

        private void bSuicide_Click(object sender, EventArgs e)
        {

            if (lbItems.SelectedItem != null)
            {
                int hoogstePos = 999999;
                int playerID = -1;
                string playername = "";

                // bepaal welke speler het hoogste staat...
                foreach (DataGridViewRow row in gPlayers.Rows)
                {
                    if (Convert.ToInt32(row.Cells["Possition"].Value) < hoogstePos)
                    {
                        if (row.Cells[0].Value != null && (bool)row.Cells[0].Value)
                        {
                            hoogstePos = Convert.ToInt32(row.Cells["Possition"].Value);
                            playerID = Convert.ToInt32(row.Cells[1].Value);
                            playername = row.Cells[2].Value.ToString();
                        }
                    }
                }

                DialogResult result = MessageBox.Show("Suicide player: " + playername + "?", "Suicide", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    _players.Suicide(playerID, false);

                    Item item = (Item)lbItems.SelectedItem;
                    DataConnector.UpdatePlayers(_players);
                    LootItem lootitem = new LootItem(-1, item.ID, DateTime.Now, false);
                    DataConnector.UpdateLootItem(lootitem, playerID);

                    Addplayers();
                }               

                tbItemFilter.Text = "";
                lbItems.SelectedItem = null;
            }
            else
            {
                MessageBox.Show("No Item selected");
            }
        }

        private void tbItemFilter_TextChanged(object sender, EventArgs e)
        {
            if (cbBoss.SelectedItem != null)
            {
                LoadItems((Boss)cbBoss.SelectedItem, tbItemFilter.Text);                
            }
            else if (cbZone.SelectedItem != null)
            {
                LoadItems((Zone)cbZone.SelectedItem, tbItemFilter.Text);
            }

            if (lbItems.Items.Count > 0)
            {
                lbItems.SelectedItem = lbItems.Items[0];
            }
        }
    }
}
